home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / misc / installer / examples / math.installer next >
Text File  |  1999-04-19  |  2KB  |  72 lines

  1.  
  2. (user expert)
  3. (message "\n\n\nWelcome to some recursive math...\n\n\n"
  4.          "Please have a look at the source to see how to\n"
  5.          "program recursive functions and how to use\n"
  6.          "local environments of procedures!")
  7.  
  8. ; --------------------------------------------------------------------------------
  9. ; define a recursive procedure which calculates the faculty of
  10. ; a given argument. use the LET function to create a local environment
  11. ; note: for arguments greater than 12 there will be an overflow,
  12. ;       but this does not result in any runtime error
  13.  
  14. (procedure faculty fac
  15.  
  16.   (let (set f fac)
  17.  
  18.        (if (= f 1)
  19.            1
  20.            (* f (faculty (- f 1)))
  21.        )
  22.   )
  23. )
  24.  
  25. ; --------------------------------------------------------------------------------
  26. ; the function "Ackerman" is a extremly recursive function, which only
  27. ; runs with local variables. thus here you must use the LET function
  28. ;
  29. ; the definition of the ackerman function is:
  30. ; a(0,y) = y+1
  31. ; a(x,0) = a(x-1,1)
  32. ; a(x,y) = a(x-1,a(x,y-1))
  33.  
  34. (procedure ackerman a b
  35.  
  36.   (let (set x a y b)
  37.  
  38.        (if (= x 0)
  39.            (+ y 1)
  40.            (if (= y 0)
  41.                (ackerman (- x 1) 1)
  42.                (ackerman (- x 1) (ackerman x (- y 1)))
  43.            )
  44.        )
  45.   )
  46. )
  47.  
  48. ; --------------------------------------------------------------------------------
  49. ; ask for a number and then calculate the fac of this number
  50.  
  51. (set fac (asknumber (prompt "Faculty of what ?")
  52.                     (help "enter the number you want to know the faculty of")
  53.                     (default 3)
  54.                     (range 1 12)
  55.          )
  56. )
  57.  
  58. ; --------------------------------------------------------------------------------
  59. ; calculate and print the faculty
  60.  
  61. (message "Faculty of " fac " = " (faculty fac)
  62.          "\n\n"
  63.          "The result of Ackerman(1,2) = " (ackerman 1 2)
  64. )
  65.  
  66. ; --------------------------------------------------------------------------------
  67. ; avoid stupid welcome...   :)
  68.  
  69. (exit (quiet))
  70. (welcome)
  71.  
  72.